﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.Data;
using System.Data;

namespace blackhouse.BlackApart.Data.Sql {
	public class ApartmentGroupDataAccess : AdoApartmentGroupDataAccess {

		#region Fields

		private string connectionString;
		private MsSqlAdoDataHelper dataHelper;

		#endregion

		#region Constructors

		public ApartmentGroupDataAccess(string sqlConnectionString) {
			this.connectionString = sqlConnectionString;
			this.dataHelper = new MsSqlAdoDataHelper(this.connectionString);
			SqlVersioning versioning = new SqlVersioning(this.connectionString, this.GetType().Assembly);
		}

		#endregion

		#region AdoApartmentGroupDataAccess Members

		protected override IDataReader GetReaderForApartmentGroupSeo(int groupId, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupSeoGet";
			this.dataHelper.AddParameter("Id", groupId);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
			IDataReader reader = this.dataHelper.Execute();
			return reader;
		}

		public override int DeleteApartmentGroup(int groupId) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupRemove";
			this.dataHelper.AddParameter("Id", groupId, System.Data.DbType.Int32);
			return this.dataHelper.ExecuteWithoutResult();
		}

		public override bool CheckExists(int groupId) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupExists";
			this.dataHelper.AddParameter("Id", groupId, System.Data.DbType.Int32);
			int count = (int)this.dataHelper.ExecuteScalar();
			return count == 1;
		}

		public override int SaveApartmentGroup(int id, string name, int numberOfPeople, int locationId, float areaSize) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupSave";
			this.dataHelper.AddParameter("Id", id, System.Data.DbType.Int32);
			this.dataHelper.AddParameter("Name", name);
			this.dataHelper.AddParameter("NumberOfPeople", numberOfPeople, System.Data.DbType.Int32);
			this.dataHelper.AddParameter("LocationId", locationId, System.Data.DbType.Int32);
			this.dataHelper.AddParameter("DefaultAreaSize", areaSize, System.Data.DbType.Single);
			return Convert.ToInt32(this.dataHelper.ExecuteScalar());
		}

		protected override IDataReader GetReaderForApartmentGroup(int groupId, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupGet";
			this.dataHelper.AddParameter("Id", groupId);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
			IDataReader reader = this.dataHelper.Execute();
			return reader;
		}

		protected override IDataReader GetReaderForApartmentGroups(string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupGetAll";
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
			IDataReader reader = this.dataHelper.Execute();
			return reader;
		}

		public override DataTable GetDtApartmentGroupProperties(int apartmentGroup, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupPropertiesGetAll";
			this.dataHelper.AddParameter("Id", apartmentGroup);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
			return this.dataHelper.ExecuteDataTable();
		}

		protected override IDataReader GetReaderForApartmentGroupArrayProperties(int apartmentGroup, string property, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupPropertiesArrayGet";
			this.dataHelper.AddParameter("PropertyKey", property);
			this.dataHelper.AddParameter("TypeId", apartmentGroup, DbType.Int32);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
			IDataReader reader = this.dataHelper.Execute();
			return reader;
		}

		protected override IDataReader GetReaderForApartmentGroupOptionFields(int groupId, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_ApartmentGroupOptions";
			this.dataHelper.AddParameter("GroupId", groupId, DbType.Int32);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength);
			return this.dataHelper.Execute();
		}

		#endregion

	}
}
